ArrayList<Student> students = new ArrayList<Student>( 30 );
Rough estimates are good enough. It is better to overestimate the required capacity than to underestimate it. A few unused slots do not hurt.
An ArrayList
object has a capacity and a size.
The capacity is the number of cells that are currently in the array.
The size is the number of cells that are filled.
To find out the current size of an ArrayList
use its
size()
method.
The size increases by one each time an element is added.
However, the capacity remains unchanged until the ArrayList
is full.
When an element is added to a full list,
the Java runtime system will greatly increase the capacity of the list
so that many more elements can be added.
(Thought Question: ) Why does the capacity increase by a large amount when an element is added to a full list? Why not just increase the capacity by one?